%
% Author: Vasyl Mykhalchuk
%
function [ h ] = entropy( data, ncell)
% h - is an entropy
h = 0.0;

[nX, nY] = size(data);
if (nY > nX)
    data = reshape(data, nY, nX);
    [nX, nY] = swap(nX, nY);
end%if

min = 0.0;
for i = 1:nX
     if ( data(i, 1) < min )
         min = data(i, 1);
     end%if
end%for
min = abs(min) + 0.1;
for i = 1:nX
    data(i, 1) = data(i, 1) + min;
end%for

[f ,xout] = hist(data, ncell);
f = f / nX;

% Compute entropy
for i = 1:ncell
    if ( f(i) == 0 )
        h = h;
    else
        h = h - f(i) * log(f(i));
    end%if
end%for
end%function entropy

function [ a, b] = swap( a, b )
    tmp = a;
    a = b;
    b = tmp;
end%function swap

